home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
UTILITY1
/
MSWSRC35.ZIP
/
INTERN.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-26
|
4KB
|
138 lines
/*
* intern.c logo data interning module dvb
*
* Copyright (C) 1989 The Regents of the University of California
* This Software may be copied and distributed for educational,
* research, and not for profit purposes provided that this
* copyright and statement are included in all such copies.
*
*/
#include "logo.h"
#include "globals.h"
NODE **hash_table;
void map_oblist(void (*fcn)(NODE *))
{
int i;
NODE *nd;
for (i = 0; i < HASH_LEN; i++)
for (nd = hash_table[i]; nd != NIL; nd = cdr(nd))
(*fcn) (car(nd));
}
int hash(char *s, int len)
/* Map S to an integer in the range 0 .. HASH_LEN-1. */
/* Method attributed to Peter Weinberger, adapted from Aho, Sethi, */
/* and Ullman's book, Compilers: Principles, Techniques, and */
/* Tools; figure 7.35. */
{
unsigned int h = 0, g;
while (--len >= 0) {
h = (h << 4) + *s++;
g = h & (0xf << (WORDSIZE-4));
if (g != 0) {
h ^= g ^ (g >> (WORDSIZE-8));
}
}
return h % HASH_LEN;
}
NODE *make_case(NODE *casestrnd, NODE *obj)
{
NODE *new_caseobj, *clistptr;
clistptr = caselistptr__object(obj);
new_caseobj = make_caseobj(casestrnd, obj);
setcdr(clistptr, cons(new_caseobj, cdr(clistptr)));
return(new_caseobj);
}
NODE *make_object(NODE *canonical, NODE *proc, NODE *val,
NODE *plist, NODE *casestrnd)
{
NODE *temp;
temp = cons_list(0, canonical, proc, val, plist,
make_intnode((FIXNUM)0), END_OF_LIST);
make_case(casestrnd, temp);
return(temp);
}
NODE *make_instance(NODE *casend, NODE *lownd)
{
NODE *obj;
int hashind;
/* Called only if arg isn't already in hash table */
obj = make_object(lownd, UNDEFINED, UNBOUND, NIL, casend);
hashind = hash(getstrptr(lownd), getstrlen(lownd));
push(obj,(hash_table[hashind]));
return car(caselist__object(obj));
}
NODE *find_instance(NODE *lownd)
{
NODE *hash_entry, *thisobj;
int cmpresult;
hash_entry = hash_table[hash(getstrptr(lownd), getstrlen(lownd))];
while (hash_entry != NIL) {
thisobj = car(hash_entry);
cmpresult = compare_node(lownd, canonical__object(thisobj), FALSE);
if (cmpresult == 0)
break;
else
hash_entry = cdr(hash_entry);
}
if (hash_entry == NIL) return(NIL);
else return(thisobj);
}
int case_compare(NODE *nd1, NODE *nd2)
{
if (backslashed(nd1) && backslashed(nd2)) {
if (getstrlen(nd1) != getstrlen(nd2)) return(1);
return(strncmp(getstrptr(nd1), getstrptr(nd2),
getstrlen(nd1)));
}
if (backslashed(nd1) || backslashed(nd2))
return(1);
return(compare_node(nd1, nd2, FALSE));
}
NODE *find_case(NODE *strnd, NODE *obj)
{
NODE *clist;
clist = caselist__object(obj);
while (clist != NIL &&
case_compare(strnd, strnode__caseobj(car(clist))))
clist = cdr(clist);
if (clist == NIL) return(NIL);
else return(car(clist));
}
NODE *intern(NODE *nd)
{
NODE *obj, *casedes, *lownd;
if (nodetype(nd) == CASEOBJ) return(nd);
nd = valref(cnv_node_to_strnode(nd));
lownd = make_strnode(getstrptr(nd), (char *)NULL,
getstrlen(nd), STRING, noparitylow_strnzcpy);
if ((obj = find_instance(lownd)) != NIL) {
if ((casedes = find_case(nd, obj)) == NIL)
casedes = make_case(nd, obj);
} else
casedes = make_instance(nd, lownd);
deref(nd);
gcref(lownd);
return(casedes);
}